package com.ruoyi.system.controller;

import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.domain.SysOrg;
import com.ruoyi.system.service.ISysOrgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 机构Controller
 *
 * @author ruoyi
 * @date 2023-02-07
 */
@Api(tags = "机构资质")
@RestController
@RequestMapping("/org")
public class SysOrgController extends BaseController {
    @Autowired
    private ISysOrgService sysOrgService;

    @Autowired
    private RedisService redisService;

    /**
     * 查看当前机构所有的列表 默认 10条
     */
    @GetMapping("/orgList")
    @ApiOperation(value = "查询机构列表", response = SysOrg.class)
    public TableDataInfo orgList(SysOrg sysOrg) {
        startPage();
        List<SysOrg> list = sysOrgService.orgList(sysOrg);
        return getDataTable(list);
    }

    /**
     * 查询机构列表
     */
    @Log(title = "登录时查询机构列表", businessType = BusinessType.INSERT)
    @RequiresPermissions("system:org:list")
    @GetMapping("/list")
    @ApiOperation(value = "查询机构列表", response = SysOrg.class)
    public TableDataInfo list(SysOrg sysOrg) {
        startPage();
        List<SysOrg> list = sysOrgService.selectSysOrgList(sysOrg);
        return getDataTable(list);
    }

    /**
     * 是否显示机构
     */
    @GetMapping("/isShow")
    @ApiOperation(value = "是否显示机构")
    public AjaxResult isShow() {
        String loginUserIsMobile = CacheConstants.SYS_CONFIG_KEY + "loginUserIsMobile";
        Boolean isValidate = Convert.toBool(redisService.getCacheObject(loginUserIsMobile));
        if (isValidate != null && isValidate) {
            return AjaxResult.success(true);
        }
        return AjaxResult.success(false);
    }

    /**
     * 导出机构列表
     */
    @RequiresPermissions("system:org:export")
    @Log(title = "导出机构列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出机构列表")
    public void export(HttpServletResponse response, SysOrg sysOrg) {
        List<SysOrg> list = sysOrgService.selectSysOrgList(sysOrg);
        ExcelUtil<SysOrg> util = new ExcelUtil<SysOrg>(SysOrg.class);
        util.exportExcel(response, list, "机构数据");
    }

    /**
     * 获取机构和资质详细信息
     */
    @RequiresPermissions("system:org:query")
    @GetMapping(value = "/{orgId}")
    @ApiOperation(value = "获取机构和资质详细信息", response = SysOrg.class)
    public AjaxResult getInfo(@ApiParam(value = "机构id", required = true) @PathVariable("orgId") Long orgId) {
        return success(sysOrgService.selectSysOrgByOrgId(orgId));
    }

    /**
     * 新增或者编辑机构和资质的信息
     */
    @Log(title = "新增或者编辑机构和资质的信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增或者编辑机构和资质的信息")
    public AjaxResult add(@RequestBody @Validated SysOrg sysOrg) {
        Map<String, String> resultMap = sysOrgService.insertSysOrg(sysOrg);
        return AjaxResult.data(resultMap);
    }

    /**
     * 逻辑删除机构
     */
    @RequiresPermissions("system:org:remove")
    @Log(title = "逻辑删除机构", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{orgIds}")
    @ApiOperation(value = "逻辑删除机构")
    public AjaxResult remove(@PathVariable Long[] orgIds) {
        return toAjax(sysOrgService.deleteSysOrgByOrgIds(orgIds));
    }
}
